package com.me.memorytest;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Vibrator;
import android.widget.Toast;

public class AndroidAPI implements IAndroidAPI
{
	MainMemoryTest mainMemoryTest;
	MainActivity mainActivity;
	public AndroidAPI()
	{
	}
	
	public AndroidAPI(MainActivity mainActivity) {
		this.mainActivity = mainActivity;
	}

	@Override
	public boolean isNetworkAvai() {
		 boolean connected = false;
		    ConnectivityManager connectivityManager = (ConnectivityManager)mainActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
		    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
		            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
		        //we are connected to a network
		        connected = true;
		    }
		    return connected;
	}


	@Override
	public void showToast(final String content) {
		mainActivity.runOnUiThread(new Runnable() {
			   public void run() {
					Toast.makeText(mainActivity.getBaseContext(), content, Toast.LENGTH_SHORT).show();
			   }
			});

	}

	@Override
	public void Vibrate() {
		Vibrator v = (Vibrator) mainActivity.getSystemService(Context.VIBRATOR_SERVICE);
		// Vibrate for 500 milliseconds
		v.vibrate(500);
	}

	@Override
	public void shareTextUrl(String Url) {
		Intent share = new Intent(android.content.Intent.ACTION_SEND);
	    share.setType("text/plain");
	    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
	    share.putExtra(Intent.EXTRA_SUBJECT, R.string.app_name);
	    share.putExtra(Intent.EXTRA_TEXT, Url);
	
	    try{
	    mainActivity.startActivity(Intent.createChooser(share, "Share Game !!!"));
	    }catch(Exception e){
	    	showToast("Please, check your internet connection");
	    }
		
	}

	@Override
	public void shareImage(String img) {
		Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        File imageFileToShare = new File(img);
        Uri uri = Uri.fromFile(imageFileToShare);
        share.putExtra(Intent.EXTRA_STREAM, uri);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try{
        	mainActivity.startActivity(Intent.createChooser(share, "Share HighScore!"));
        }catch(Exception e){
        	showToast("Please, check your internet connection");
        }
	}
}